package cn.consult.controller.sys.park;

import cn.consult.common.api.CommonPage;
import cn.consult.common.api.CommonResult;
import cn.consult.sys.entity.TApartmentApplyfor;
import cn.consult.sys.entity.TDwellEmployee;
import cn.consult.sys.entity.TPlaceBuilding;
import cn.consult.sys.service.TApartmentApplyforService;
import cn.consult.sys.service.TDwellEmployeeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.Map;

@RestController
@Api(tags = "园区管理 > 公寓申请审批")
@RequestMapping("/park/examine")
public class TApartmentApplyforController {

    @Autowired
    TApartmentApplyforService tApartmentApplyforService;
    @Autowired
    TDwellEmployeeService tDwellEmployeeService;

    @ApiOperation("getExamineAll =>  条件查询审批记录")
    @GetMapping("/getExamineAll")
    public CommonResult getExamineAll(@RequestParam Map<String, Object> params) {
        IPage<TApartmentApplyfor> page = tApartmentApplyforService.getPage(params);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("updateStatus =>  修改状态")
    @PutMapping("/updateStatus")
    public CommonResult updateStatus(@RequestBody TApartmentApplyfor tApartmentApplyfor) {
        tApartmentApplyforService.updateById(tApartmentApplyfor);

        if (tApartmentApplyfor.getJournalNumber() != null) {
            TDwellEmployee tDwellEmployee = new TDwellEmployee();
            tDwellEmployee.setId(Long.valueOf(tApartmentApplyfor.getJournalNumber()));
            tDwellEmployee.setLeaseStatus(String.valueOf(2));


            TDwellEmployee byIdId = tDwellEmployeeService.getByIdId(Long.valueOf(tApartmentApplyfor.getJournalNumber()));
            LocalDate localDate = LocalDate.parse(byIdId.getCheckInTime());
            LocalDate newDateAdd = localDate.plusMonths(Long.parseLong(byIdId.getTenancyTerm()));
            tDwellEmployee.setLeaseDeadline(String.valueOf(newDateAdd));

            tDwellEmployeeService.updateById(tDwellEmployee);

        }
        if (tApartmentApplyfor.getApplyforStatus() == "2") {
            return CommonResult.success("审核通过");

        } else {
            return CommonResult.success("审核已拒绝");
        }
    }

    @ApiOperation("addExamine =>  添加审批")
    @PostMapping("/addExamine")
    public CommonResult addExamine(@RequestBody TApartmentApplyfor tApartmentApplyfor) throws ParseException {

        boolean flag = tApartmentApplyforService.saveExamine(tApartmentApplyfor);
        return CommonResult.success(flag);
    }

    @ApiOperation("getExamineById =>  根据id查询审批详情")
    @GetMapping("/getExamineById/{id}")
    public CommonResult getExamineById(@PathVariable Long id) {
        TApartmentApplyfor byId = tApartmentApplyforService.getById(id);
        return CommonResult.success(byId);
    }

    @ApiOperation("deleteExamine =>  删除审批")
    @DeleteMapping("/deleteExamine/{id}")
    public CommonResult deleteExamine(@PathVariable Long id) {
        boolean b = tApartmentApplyforService.removeById(id);
        return CommonResult.success(b);
    }


}
